﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Windows.Input;
using KSATouch;
using KSASupport;
using System.Windows.Shapes;


namespace KSAWPFExtensions
{
    /// <summary>
    /// Represents a WPF window which can recieve touch events from KSA
    /// </summary>
    public class KSAWPFWindow : Window
    {
        private KSATouch.KSATouch ksaTouch;

        public KSATouch.KSATouch KsaTouch
        {
            get { return ksaTouch; }
            set { ksaTouch = value; }
        }

        public event FrameReadyEventHandler FrameReady;
        public virtual void OnFrameReady(object sender, FrameReadyEventArgs e)
        {
            if (FrameReady != null)
            {
                e.CurrentContacts  = ksaTouch.CurrentContacts;
                e.HoveringContacts = ksaTouch.CurrentHoveringContacts;
                FrameReady(this, e);
            }
        }

        /// <summary>
        /// TODO:Fixup naming
        /// </summary>
        public event OrderRecievedEventHandler VoiceCommandRecognized;
        public virtual void OnVoiceCommandRecognized(string VoiceCommand)
        {
            if (VoiceCommandRecognized != null)
            {
                VoiceCommandRecognized(VoiceCommand);
            }
        }
        
        public KSAWPFWindow()
        {
            try
            {
                ksaTouch = new KSATouch.KSATouch();
                ksaTouch.DepthManagers[0].Enabled = true;
                
                ksaTouch.ContactDown += ksaTouch_ContactDown;
                ksaTouch.ContactChanged += ksaTouch_ContactChanged;
                ksaTouch.ContactUp += ksaTouch_ContactUp;
                ksaTouch.FrameReady += ksaTouch_FrameReady;
                ksaTouch.OrderRecieved += ksaTouch_OrderRecieved;
            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.WriteLine("Kinect initialization failed..." + exc.Message);
                this.WindowStyle = System.Windows.WindowStyle.ThreeDBorderWindow;
                this.Title = "Kinect not detected";

            }
        }
        /// <summary>
        /// Fires when a voice command has been recieved
        /// </summary>
        /// <param name="Order"></param>
        void ksaTouch_OrderRecieved(string Order)
        {
            OnVoiceCommandRecognized(Order);
        }

        /// <summary>
        /// Fires when a touch contact is removed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ksaTouch_ContactUp(object sender, ContactEventArgs e)
        {
            KSAWPFTouchDevice.KSAContact_Up(this, e);
        }

        /// <summary>
        /// Fires when a touch contact is moved
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ksaTouch_ContactChanged(object sender, ContactEventArgs e)
        {
            KSAWPFTouchDevice.KSAContact_Changed(this, e);
        }

        /// <summary>
        /// Fires when a touch contact is first placed down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ksaTouch_ContactDown(object sender, ContactEventArgs e)
        {
            KSAWPFTouchDevice.KSAContact_Down(this, e);
        }

        /// <summary>
        /// Fires when each frame is processed. Can be queried for current touch or hovering contacts
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="e"></param>
        void ksaTouch_FrameReady(object Sender, FrameReadyEventArgs e)
        {
            OnFrameReady(this, e);
        }



        void kinectScreenAware_ContactUp(object sender, ContactEventArgs e)
        {
            try
            {
                KSAWPFTouchDevice.KSAContact_Up(this, e);
            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.WriteLine("Exception on contactup!" + exc.Message);
            }
        }

        void kinectScreenAware_ContactChanged(object sender, ContactEventArgs e)
        {
            KSAWPFTouchDevice.KSAContact_Changed(this, e);
        }

        void kinectScreenAware_ContactDown(object sender, ContactEventArgs e)
        {
            KSAWPFTouchDevice.KSAContact_Down(this, e);
        }

    }
}
